<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="shadow"></div>
</body>
<script>
    // 需求: 元素拖拽  =>  拖拽功能 => 
    // var 拖拽 = {
    //     target,
    //     dragStart,
    //     dragMove,
    //     dragEnd,
    // }





    /*  var shadow = document.getElementsByClassName("shadow")[0];
 
     shadow.onmousedown = function (e) {
         var e = e || window.event;
 
         var posX = e.offsetX;
         var posY = e.offsetY;
         console.log(posX, posY);
 
         //  先按下 再移动 => 按下时绑定移动事件  
         document.onmousemove = function (e) {
             var e = e || window.event;
             var x = e.pageX - posX;
             var y = e.pageY - posY;
             console.log(x, y);
 
             shadow.style.left = x + "px";
             shadow.style.top = y + "px";
         }
         document.onmouseup = function () {
             document.onmousemove = null;
             document.onmouseup = null;
         }
     } */

    // 问题1:  posX  posY 变量跨作用域 => 提取到两个作用域都能访问的位置(dragEle)
    // 问题2: 如何将posX posY存储到dragEle?


    var dragEle = {
        target: document.getElementsByClassName("shadow")[0],
        dragStart: function () { // dragEle.dragStart();
            // this -> dragEle对象

            // 多个函数嵌套  this指向混乱的问题
            // 1. 提前声明中间变量记录this的指向   _this = this; that = this;
            // 2. 借助箭头函数

            var _this = this; // _this = dragEle    提前声明中间变量记录this的指向  

            // 拖拽开始 在目标元素上按下 记录按下的位置 
            this.target.onmousedown = function (e) {
                // this-> 指向绑定事件的元素 shadow

                var e = e || window.event;
                var posX = e.offsetX;
                var posY = e.offsetY;
                console.log(posX, posY);
                _this.posX = posX;
                _this.posY = posY;
                console.log(_this);  // 自己没有找父作用域 =>  dragEle 

                // 先按下 => 按下时绑定移动和松开事件
                _this.dragMove();
                _this.dragEnd();
            }
        },
        dragMove: function () { //  dragEle.dragMove();
            // 鼠标在文档中移动时,元素跟随鼠标移动
            // this -> dragEle对象 

            document.onmousemove = () => {
                // 箭头函数  => this(依赖于父作用域的this执行 this->dragEle)
                var e = e || window.event;
                var x = e.pageX - this.posX;
                var y = e.pageY - this.posY;
                console.log(x, y);

                this.target.style.left = x + "px";
                this.target.style.top = y + "px";

            }
        },
        dragEnd: function () {
            // 鼠标在文档中任意位置松开时  清除移动
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            }

        }
    }

    // 同时绑定三个
    // dragEle.dragStart();
    // dragEle.dragMove();
    // dragEle.dragEnd();


    dragEle.dragStart();










</script>

</html>